//Global variable
var pa_pageSize = 12;
var pa_currentPage = 1;
allVillage = [];
var provinceAllTotalPage = null;

//initialize page
(function() {
	//添加文本框的click事件
	$(".city_input").click(function(event) {
		//取得当前文本框的位置
		var o2 = $(this).offset();
		var left2 = o2.left;
		var top2 = o2.top;
		var h2 = $(this).height();
		
		//设置弹出地址的div的位置
		//toggle() 方法切换元素的可见状态。如果被选元素可见，则隐藏这些元素，如果被选元素隐藏，则显示这些元素。
		$(".provinceCityAll").css("top", top2 + h2 - 39.5).css("left", left2-255).toggle();
		$(".provinceCityAll").click(function(event) {
			event.stopPropagation();
		});
		//阻止事件的默认行为
		event.stopPropagation();
		
		//页面点击任何按钮，将隐藏地址选择div
		$("html").click(function() {
			$(".provinceCityAll").hide();
		});
		
		//获取文本框对象,移除css current2
		$("input.city_input").removeClass("current2");
		
		//添加current2 css到该文本框上
		$(this).addClass("current2");
		
		//移除tabs下的所有a节点的current css
		$(".provinceCityAll").find(".tabs").find("a").removeClass("current");
		//选中
		$(".provinceCityAll").find(".tabs").find("a[tb=provinceAll]").addClass("current");
		
		//隐藏con css的div的所有内容
		$(".provinceCityAll").find(".con").children().hide();
		
		//显示con下hot city的内容
		$(".provinceCityAll").find(".con").find(".provinceAll").show();
		
		//是否取得所有的省，直辖市的内容
		//if ($("body").data("allProvinces") == null) {
			//发生ajax请求，取得所有的省，直辖市的内容
			sendAllProvinceAjax();
		//}
		
		//添加点击tab（热门城市,省份，城市,区县,街道）的click事件
		$(".provinceCityAll").find(".tabs").find("a").click(function() {
			//如果点击的是城市，并且当前已经选择的是省，就什么也不做
			/*if ($(this).attr("tb") == "cityAll" && $(".provinceAll .list .current").val() == null) {
				return;
			};
			//如果点击的是区县，并且当前已经选择的城市，再并且热门城市未选择，就什么也不做
			if ($(this).attr("tb") == "countyAll"  && $(".cityAll .list .current").val() == null && $(".hotCityAll .list .current").val() == null) {
				return;
			};

			//移除之前选中的tab
			//village 
			if ($(this).attr("tb") == "villageAll" && $(".countyAll .list .current").val() == null && $(".hotCityAll .list .current").val() == null) {
				return;
			};*/
			$(".provinceCityAll").find(".tabs").find("a").removeClass("current");
			//把当前点击的tab选中
			$(this).addClass("current");
			
			var tb = $(this).attr("tb");
			//隐藏con下所有的内容
			$(".provinceCityAll").find(".con").children().hide();
			//显示当前选中的内容
			$(".provinceCityAll").find(".con").find("." + tb).show();
		});
	});
})(); 

//取得所有的省，直辖市
function sendAllProvinceAjax() {
	$.ajax({
		type: "get",
		url: $("#rootPath").text()+'/ltms/region/query/0',
		dataType: "json",
		async: false,
		success: function(data) {
			allProvinces = data;
			$("body").data("allProvinces", allProvinces);
			
			viewAllProvince(1);
		},
		error: function(XMLHttpRequest, textStatus, errorThrown)
		 {
			alert(textStatus);
		}
	});
}

function viewAllProvince(page) {
	$(".provinceAll .list ul li").remove();
	if (page == 1) {
		$(".provinceAll .pre a").removeClass("can");
		$(".provinceAll .next a").addClass("can");
	} else {
		$(".provinceAll .pre a").addClass("can");
		$(".provinceAll .next a").addClass("can");
	}
	var end;
	var start;
	if (page == provinceAllTotalPage) {
		start = (page - 1) * pa_pageSize;
		end = allProvinces.length;
		$(".provinceAll .next a").removeClass("can");
	} else {
		start = (page - 1) * pa_pageSize;
		end = start + pa_pageSize;
	}
	for (var i = start; i < end; i++) {
		var p_id = allProvinces[i].id;
		var p_name = allProvinces[i].locationName;
		if (p_name == '内蒙古自治区') {
			p_name = '内蒙古';
		} else if (p_name == '黑龙江省') {
			p_name = '黑龙江';
		} else {
			p_name = p_name.substr(0, 2);
		}
		var li = $('<li><a style="background: none repeat scroll 0% 0% transparent; border: 0px none;" href="javascript:onclick=viewAllCities(' + i + ');" id="' + p_id + '">' + p_name + '</a></li>');
		$(".provinceAll .list ul").append(li);
	}
	$(".provinceAll .list #provincePage1").remove();
	$(".provinceAll .list").append("<label id='provincePage1' style='display:none;'>" + page + "</label>");
}

function viewAllCities(i) {
	allProId = allProvinces[i].id;
	$("body").data("pAllName", allProvinces[i].locationName);
	$("body").data("pAllId", allProId);
	
	$("#regionId").val(allProId);
	$(".city_input").val(allProvinces[i].locationName);
	
	sendAllCitiesAjax(allProId);
}

//城市
function sendAllCitiesAjax(p_id) {
	$.ajax({
		type: "get",
		url: $("#rootPath").text()+'/ltms/region/query/'+p_id,
		async: false,
		dataType: "json",
		success: function(data) {
			allCities = allCitys = data;
			$("body").data("CitysAll", data);
			//viewAllHotCities();
			allCityTotalPage = Math.ceil(allCitys.length / pa_pageSize);
			$(".provinceCityAll").find(".tabs").find("a").removeClass("current");
			$(".provinceCityAll .tabs").find("#cityAll").addClass("current");
			$(".con .provinceAll .list a").removeClass("current");
			$(".con .provinceAll .list a[id='" + p_id + "']").addClass("current");
			$(".provinceCityAll").find(".con").children().hide();
			$(".provinceCityAll").find(".con").find(".cityAll").show();
			
			allCityPage(1);			
		},
		error: function(XMLHttpRequest, textStatus, errorThrown)
		 {
			alert(textStatus);
		}
	});
}

function allCityPage(page) {
	$(".cityAll .list ul li").empty();
	$(".cityAll .list ul li").remove();
	if (page == 1) {
		$(".cityAll .pre a").removeClass("can");
	} else {
		$(".cityAll .pre a").addClass("can");
	}
	var start;
	var end;
	if (page <= 1) {
		page = 1;
		$(".cityAll .pre a").removeClass("can");
		$(".cityAll .next a").addClass("can");
	}
	if (allCityTotalPage == 1) {
		$(".cityAll .next a").removeClass("can");
		$(".cityAll .pre a").removeClass("can");
	}
	if (page >= allCityTotalPage) {
		page = allCityTotalPage;
		$(".cityAll .next a").removeClass("can");
		start = (page - 1) * pa_pageSize;
		end = allCitys.length;
	} else if (page == 1) {
		start = (page - 1) * pa_pageSize;
		end = start + pa_pageSize;
		$(".cityAll .pre a").removeClass("can");
		$(".cityAll .next a").addClass("can");
	} else {
		start = (page - 1) * pa_pageSize;
		end = start + pa_pageSize;
		$(".cityAll .next a").addClass("can");
		$(".cityAll .pre a").addClass("can");
	}
	for (var i = start; i < end; i++) {
		var c_id = allCitys[i].id;
		var cityName = allCitys[i].locationName.substr(0,2);
		var li = $('<li><a href="javascript:onclick=viewAllCounties(' + i + ')" id="' + c_id + '">' + cityName + '</a></li>');
		$(".cityAll .list ul").append(li);
	}
	$(".cityAll .list #cityPage1").remove();
	$(".cityAll .list").append("<label id='cityPage1' style='display:none;'>" + page + "</label>");
}

function viewAllCounties(i) {
	cityIdAll = allCitys[i].id;
	$("body").data("cityName", $.trim(allCitys[i].locationName));
	$("body").data("cityId", cityIdAll);
	
	$("#regionId").val(cityIdAll);
	//取得省的名字
	var provinceName = $("body").data("pAllName");
	//取得市的名字
	var cityName = $("body").data("cityName");
	$(".city_input").val(provinceName+"-"+cityName);
	
	sendAllCountiesAjax(cityIdAll);
}

//区县
function sendAllCountiesAjax(p_id)
 {
	$.ajax({
		type: "get",
		url: $("#rootPath").text()+'/ltms/region/query/'+p_id,
		async: false,
		dataType: "json",
		success: function(data) {
			allAreas = countiesAll = data;
			$("body").data("allCountys", data);
			
			countyTotalPageAll = Math.ceil(countiesAll.length / pa_pageSize);
			$(".provinceCityAll").find(".tabs").find("a").removeClass("current");
			$(".provinceCityAll .tabs").find("#countyAll").addClass("current");
			$(".con .cityAll .list a").removeClass("current");
			$(".con .cityAll .list a[id='" + p_id + "']").addClass("current");
			$(".provinceCityAll").find(".con").children().hide();
			$(".provinceCityAll").find(".con").find(".countyAll").show();
			allCountyPage(1);			
		},
		error: function(XMLHttpRequest, textStatus, errorThrown)
		 {
			alert(textStatus);
		}
	});
}

function allCountyPage(page) {
	var nameOfProvince = $("body").data("pAllName");
	var cityCurrentName = $("body").data("nameOfCityAll");
	//$("input.current2").removeClass("iGrays");
	//$("input.current2").val(nameOfProvince + "-" + cityCurrentName);
	$(".countyAll .list ul li").remove();
	if (page == 1) {
		$(".countyAll .pre a").removeClass("can");
	} else {
		$(".countyAll .pre a").addClass("can");
	}
	var start;
	var end;
	if (page <= 1) {
		page = 1;
		$(".countyAll .pre a").removeClass("can");
		$(".countyAll .next a").addClass("can");
	}
	if (countyTotalPageAll == 1) {
		$(".countyAll .next a").removeClass("can");
		$(".countyAll .pre a").removeClass("can");
	}
	if (page >= countyTotalPageAll) {
		page = countyTotalPageAll;
		$(".countyAll .next a").removeClass("can");
		start = (page - 1) * pa_pageSize;
		end = countiesAll.length;
	} else if (page == 1) {
		start = (page - 1) * pa_pageSize;
		end = start + pa_pageSize;
		$(".countyAll .pre a").removeClass("can");
		$(".countyAll .next a").addClass("can");
	} else {
		start = (page - 1) * pa_pageSize;
		end = start + pa_pageSize;
		$(".countyAll .next a").addClass("can");
		$(".countyAll .pre a").addClass("can");
	}
	for (var i = start; i < end; i++) {
		var c_id = countiesAll[i].id;
		var countyName = countiesAll[i].locationName.substr(0, 4);;
		var li = $('<li><a href="javascript:onclick=viewAllVillage(' + i + ')" id="' + c_id + '">' + countyName + '</a></li>');
		$(".countyAll .list ul").append(li);
	}
	$(".countyAll .list #countyPage1").remove();
	$(".countyAll .list").append("<label id='countyPage1' style='display:none;'>" + page + "</label>");
}

function viewAllVillage(i) {
	villageIdAll = countiesAll[i].id;
	$("body").data("countyId", villageIdAll);
	var villagename = $.trim(countiesAll[i].locationName);
	$("body").data("coutryName", villagename);
	
	$("#regionId").val(villageIdAll);
	//取得省的名字
	var provinceName = $("body").data("pAllName");
	//取得市的名字
	var cityName = $("body").data("cityName");
	//取得区县的名字
	var countryName = $("body").data("coutryName");
	$(".city_input").val(provinceName+"-"+cityName+"-"+countryName);
	
	sendAllVillageAjax(villageIdAll);
}

//街道
function sendAllVillageAjax(p_id) {
	$.ajax({
		type: "get",
		url: $("#rootPath").text()+'/ltms/region/query/'+p_id,
		async: false,
		dataType: "json",
		success: function(data) {
			allVillage = data;
			$("body").data("allVillage", data);
			
			countyTotalPageAll = Math.ceil(allVillage.length / pa_pageSize);
			$(".provinceCityAll").find(".tabs").find("a").removeClass("current");
			$(".provinceCityAll .tabs").find("#villageAll").addClass("current");
			$(".con .countyAll .list a").removeClass("current");
			$(".con .countyAll .list a[id='" + p_id + "']").addClass("current");
			$(".provinceCityAll").find(".con").children().hide();
			$(".provinceCityAll").find(".con").find(".villageAll").show();
			allVillagePage(1);			
		},
		error: function(XMLHttpRequest, textStatus, errorThrown)
		 {
			alert(textStatus);
		}
	});
}

function allVillagePage(page) {
	var nameOfProvince = $("body").data("pAllName");
	var cityCurrentName = $("body").data("nameOfCityAll");
	//$("input.current2").removeClass("iGrays");
	//$("input.current2").val(nameOfProvince + "-" + cityCurrentName);
	$(".villageAll .list ul li").remove();
	if (page == 1) {
		$(".villageAll .pre a").removeClass("can");
	} else {
		$(".villageAll .pre a").addClass("can");
	}
	var start;
	var end;
	if (page <= 1) {
		page = 1;
		$(".villageAll .pre a").removeClass("can");
		$(".villageAll .next a").addClass("can");
	}
	if (countyTotalPageAll == 1) {
		$(".villageAll .next a").removeClass("can");
		$(".villageAll .pre a").removeClass("can");
	}
	if (page >= countyTotalPageAll) {
		page = countyTotalPageAll;
		$(".villageAll .next a").removeClass("can");
		start = (page - 1) * pa_pageSize;
		end = allVillage.length;
	} else if (page == 1) {
		start = (page - 1) * pa_pageSize;
		end = start + pa_pageSize;
		$(".villageAll .pre a").removeClass("can");
		$(".villageAll .next a").addClass("can");
	} else {
		start = (page - 1) * pa_pageSize;
		end = start + pa_pageSize;
		$(".villageAll .next a").addClass("can");
		$(".villageAll .pre a").addClass("can");
	}
	for (var i = start; i < end; i++) {
		var c_id = allVillage[i].id;
		var countyName = allVillage[i].locationName.substr(0, 4);;
		var li = $('<li><a href="javascript:onclick=addrInputAll(' + i + ')" id="' + c_id + '">' + countyName + '</a></li>');
		$(".villageAll .list ul").append(li);
	}
	$(".villageAll .list #villagePage1").remove();
	$(".villageAll .list").append("<label id='villagePage1' style='display:none;'>" + page + "</label>");
}

function addrInputAll(i) {
	var villageId = $.trim(allVillage[i].id);
	var villageName = $.trim(allVillage[i].locationName);
	$(".con .villageAll .list a").removeClass("current");
	$(".con .villageAll .list a[id='" + villageId + "']").addClass("current");
	
	$(".provinceCityAll").hide();
	//此值需要传送到后台使用
	$("#regionId").val(villageId);
	//取得省的名字
	var provinceName = $("body").data("pAllName");
	//取得市的名字
	var cityName = $("body").data("cityName");
	//取得区县的名字
	var countryName = $("body").data("coutryName");

	$(".city_input").val(provinceName+"-"+cityName+"-"+countryName+"-"+villageName);
	
}


//#########翻页#########
//省
$(".provinceAll .pre a").bind('click',
function() {
	var provincePage1 = parseInt($('#provincePage1').html());
	if (provincePage1 == 1) {
		return;
	}
	viewAllProvince(provincePage1 - 1);
});
$(".provinceAll .next a").bind('click',
function() {
	var provincePage1 = parseInt($('#provincePage1').html());
	provinceAllTotalPage = countAllProvincePages();
	if (provincePage1 >= provinceAllTotalPage) {
		return;
	}
	viewAllProvince(provincePage1 + 1);
});
//市
$(".cityAll .pre a").bind('click',
function() {
	var cityPages1 = parseInt($('#cityPage1').html());
	if (cityPages1 == 1) {
		return;
	}
	allCityPage(cityPages1 - 1);
});
$(".cityAll .next a").bind('click',
function() {
	if ($(this).hasClass("can")) {
		var cityPages1 = parseInt($('#cityPage1').html());
		allCityPage(cityPages1 + 1);
	}
});
//区县
$(".countyAll .pre a").bind('click',
function() {
	var countyPages = parseInt($('#countyPage1').html());
	if (countyPages == 1) {
		return;
	}
	allCountyPage(countyPages - 1);
});

$(".countyAll .next a").bind('click',
function() {
	if ($(this).hasClass("can")) {
		var countyPages = parseInt($('#countyPage1').html());
		allCountyPage(countyPages + 1);
	}
});
//街道
$(".villageAll .pre a").bind('click',
function() {
	var villagePages = parseInt($('#villagePage1').html());
	if (villagePages == 1) {
		return;
	}
	allVillagePage(villagePages - 1);
});

$(".villageAll .next a").bind('click',
function() {
	if ($(this).hasClass("can")) {
		var villagePages = parseInt($('#villagePage1').html());
		allVillagePage(villagePages + 1);
	}
});



//Helper function
function countAllProvincePages() {
	provinceAllTotalPage = Math.ceil(allProvinces.length / pa_pageSize);
	return provinceAllTotalPage;
}

